/* vim:ft=objc:
 * $Id: ActivityViewController.m 142 2005-05-22 21:18:07Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

#import "ActivityViewController.h"
#import "ActivityController.h"
#import "debug.h"

@implementation ActivityViewController

- (IBAction)ceaseActivity:(id)sender
{
	[stopTarget performSelector: activeSelector withObject: stopArgument];
}


- (id) initWithStopSelector: (SEL) stopSel
	      andGoSelector: (SEL) goSel
		     target: (id) target
		   userInfo: (id) ui
{
	if ( (self = [super init]) != Nil ) {
		if ( ![NSBundle loadNibNamed: @"ActivityView" owner: self] ) {
			alert("Unable to load ActivityView nib!\n");
			[self release];
			self = nil;
		} else {
			stopSelector = stopSel;
			goSelector = goSel;
			activeSelector = goSel;
			[stopButton setImage: [NSImage imageNamed: @"run.tiff"]];
			stopTarget = target;
			stopArgument = ui;
		}
	}
	return self;
}


- (void) awakeFromNib
{
	[self completed];
}


- (void) dealloc
{
	[subView release];
	[super dealloc];
}


- (NSView*) view
{
	return subView;
}


- (void) updateStatus: (double) pos withText: (NSString*) text
{
	[progressBar setDoubleValue: pos];
	[progressBar startAnimation: Nil];
	[activityTextField performSelectorOnMainThread:
			      @selector(setStringValue:)
		withObject: text waitUntilDone: YES];
	if ( activeSelector != stopSelector ) {
		activeSelector = stopSelector;
		[stopButton setImage: [NSImage imageNamed: @"stop.tiff"]];
	}
}


- (void) incrementStatus: (double) pos withText: (NSString*) text
{
	[progressBar incrementBy: pos];
	[progressBar startAnimation: Nil];
	[activityTextField performSelectorOnMainThread:
			      @selector(setStringValue:)
		withObject: text waitUntilDone: YES];
	if ( activeSelector != stopSelector ) {
		activeSelector = stopSelector;
		[stopButton setImage: [NSImage imageNamed: @"stop.tiff"]];
	}
}


- (void) completed
{
	activeSelector = goSelector;
	[stopButton setImage: [NSImage imageNamed: @"run.tiff"]];
	[activityTextField performSelectorOnMainThread:
			      @selector(setStringValue:)
		withObject: @"Idle" waitUntilDone: YES];
	[progressBar setDoubleValue: 0.0];
	[progressBar stopAnimation: Nil];

	[activityControl performSelectorOnMainThread:
			@selector(reloadTableView:)
		withObject: Nil waitUntilDone: NO];
}


@end
